package hsmgpslogger.server;

import hsmgpslogger.server.DA.PositionInfoDA;
import hsmgpslogger.shared.PositionInfo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class positionWriter extends HttpServlet {
	private static final long serialVersionUID = -7168885442104875300L;

	public final void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		doRequest(req, res);
	}

	public final void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		doRequest(req, res);
	}

	private void doRequest(HttpServletRequest req, HttpServletResponse res)
	{
		PositionInfo pi = new PositionInfo();
		pi.setLat(Double.parseDouble((String) req.getParameter("lat")));
		pi.setLon(Double.parseDouble((String) req.getParameter("lon")));
		pi.setAlt(Double.parseDouble((String) req.getParameter("alt")));
		
		PositionInfoDA.SavePositionInfo(pi);
		//http://127.0.0.1:8888/hsmgpslogger/positionWriter?lat=100&lon=222&alt=35
		//http://127.0.0.1:8888/hsmgpslogger/positionWriter?lat=50.412228&lon=30.510779&alt=35
		try {
			res.setContentType("text/html; charset=windows-1251");
			PrintWriter out = res.getWriter();
			out.println("ok");
			out.println(pi.toString());
		} catch (Exception e) {
			Logger.getLogger("default").log(Level.SEVERE,"Exception in error():" + e);
		}
	}
}
